package sdfi.lee.Properties;

import java.io.ByteArrayInputStream;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import sdfi.lee.DAO.DAO;
import sdfi.lee.Entity.Notice;
import sdfi.lee.Entity.Yiqishare;
import sdfi.lee.Logger.MyLogger;

public class ReadXMLFromString {
	public String source=null;
	SAXReader saxread=null;
	public  ReadXMLFromString(String source)
	{
		this.source=source;
	}
	public String getSource() {
		return source;
	}
	public void setSource(String source) {
		this.source = source;
	}
	public SAXReader getSaxread() {
		return saxread;
	}
	public void setSaxread(SAXReader saxread) {
		this.saxread = saxread;
	}
	public ReadXMLFromString(){};
	public Yiqishare readerYiqiFromString(){
		
		
		if(source==null) throw new NullPointerException();
		Yiqishare yiqi=new Yiqishare();
		try
		{
		yiqi.setRelation(this.readerValueByNode("/profile/CE[@name='CeContactPerson']/NE/@name"));
		yiqi.setUserfor(this.readerValueByNode("/profile/CE[@name='CeEffect']/NE/@name"));
		yiqi.setPhone(this.readerValueByNode("/profile/CE[@name='CeContactNum']/NE/@name"));
		yiqi.setFactory(this.readerValueByNode("/profile/CE[@name='CeManufacturers']/NE/@name"));
		yiqi.setUnit(this.readerValueByNode("/profile/CE[@name='CeOwnerOrg']/NE/@name"));
		yiqi.setYiqiname(this.readerValueByNode("/profile/@NE"));
		yiqi.setUnit (this.readerValueByNode("/profile/CE[@name='CeOwnerOrg']/NE/@name"));
		yiqi.setYiqitype(this.readerValueByNode("/profile/CE[@name='CeModel']/NE/@name"));
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			MyLogger.Logger(ee.getMessage(), null, "error");
		}
		return yiqi;
	}
	public String readerValueByNode(String xpath) 
	{
		if(saxread==null){
			this.saxread=new SAXReader();
	
		this.saxread.setEncoding("utf-8");
	
		}
		String value="";
		try
		{
			Document doc=this.saxread.read(new ByteArrayInputStream(this.source.getBytes("UTF-8")),"utf-8");
			Element root=doc.getRootElement();
			value=root.valueOf(xpath);
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			value=null;
		}
		return value;
	
	}
	public String readerValueByBode(boolean isyiqi,int id) throws Exception, DocumentException
	{
		if(saxread==null){
			this.saxread=new SAXReader();
	
		this.saxread.setEncoding("utf-8");
		}
		Document doc=this.saxread.read(new ByteArrayInputStream(this.source.getBytes("utf-8")),"utf-8");
		Element rootEl=doc.getRootElement();
		Iterator it= rootEl.elementIterator();
		String documentid=this.readerValueByNode("/profile/@documentID");
		StringBuffer sb=new StringBuffer();
		if(isyiqi)
		{
			while(it.hasNext())
			{
				Element next=(Element)it.next();
				if(next.getName().equals("type")) continue;
				int nc=next.nodeCount();
				if(nc==0) continue;
				if(nc==1){
					String tp=next.node(0).valueOf("@name");
					if(tp==null||"".endsWith(tp)) continue;
				}
				Iterator itnode=next.elementIterator();
				
				String mapvalue;
				Pattern p=Pattern.compile("[\u4e00-\u9fa9]+");
				Matcher m=p.matcher(next.valueOf("@name"));
				if(m.matches())
				{mapvalue=next.valueOf("@name");
					
					
						
				}else
				{
					String sql="select cnname from relation where name='"+next.valueOf("@name")+"'";
					mapvalue=DAO.getValueByexecuteSQL(sql, "cnname");//获取CE
					
				}
				
				sb.append("<fieldset><legend>"+mapvalue+"</legend>");
				Set<String> set=new HashSet<String>();
				//获取CE下所有的NE
				while(itnode.hasNext())
				{
					
					Element nextne=(Element)itnode.next();
					String v=nextne.valueOf("@name");
					if(!set.contains(v))
					{
					sb.append("<div style='float:left;margin-right:10px;padding:2px;'><span id='"+id+"' value='"+documentid+"'>"+v);
					set.add(v);
					sb.append("</span></div>");
					}
				}
				sb.append("</fieldset><div style='clear:both'></div>");
				
			}
		}
		else//非医疗器械
		{
			while(it.hasNext())
			{
				Element next=(Element)it.next();
				if(next.getName().equals("type")) continue;
				int nc=next.nodeCount();
				if(nc==0) continue;
				if(nc==1){
					String tp=next.node(0).valueOf("@name");
					if(tp==null||"".endsWith(tp)) continue;
				}
				
				String mapvalue;
				Pattern p=Pattern.compile("[\u4e00-\u9fa9]+");
				Matcher m=p.matcher(next.valueOf("@name"));
				if(m.matches())
				{
					mapvalue=next.valueOf("@name");
				}
				else
				{
				String sql="select cnname from relation where name='"+next.valueOf("@name")+"'";
				mapvalue=DAO.getValueByexecuteSQL(sql, "cnname");
				}
				sb.append("<fieldset><legend>"+mapvalue+"</legend>");
				Set<String> set=new HashSet<String>();
				Iterator itnode=next.elementIterator();//获取CE下所有的NE
				while(itnode.hasNext())
				{
					
					Element nextne=(Element)itnode.next();
					String v=nextne.valueOf("@name");
					if(!"无".equals(v)&&!"不详".equals(v))
					if(!set.contains(v))
					{
					sb.append("<div style='float:left;margin-right:10px;padding:2px;'><span id='"+id+"' value='"+documentid+"'>"+v);
					set.add(v);
					sb.append("</span></div>");
					}
					
				}
				sb.append("</fieldset>");
			}
		
		}
		return sb.toString();
	}
	public Notice readerPolicyFromString(){
		Notice notice=new Notice();
		try
		{
			notice.setNoticetitle(this.readerValueByNode("/root/title"));
			notice.setDate(this.readerValueByNode("/root/crawltime"));
			notice.setNoticecontent(this.readerValueByNode("/root/body"));
			
			
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			notice=null;
		}
		return notice;
	}
	public Notice readerNewsFromString(){
		Notice news=new Notice();
		try
		{
			/*2011-08-12 02:34:26*/
			news.setNoticetitle(this.readerValueByNode("/root/title"));
			news.setNoticedate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(this.readerValueByNode("")));
			news.setNoticecontent(this.readerValueByNode("/root/body"));
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			news=null;
		}
		return news;
	}
	public Notice readerNoticeFromString(){
		Notice notice=new Notice();
		try
		{
			notice.setNoticetitle(this.readerValueByNode("/root/title"));
			notice.setDate(this.readerValueByNode("/root/crawltime"));
			notice.setNoticecontent(this.readerValueByNode("/root/body"));
			
			
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			notice=null;
		}
		return notice;
	}


	public static void main(String args[])
	{
String t="<root>" +
		"<sourceUrl>http://211.157.104.87:8080/sipo/zljs/hyjs-yx-new.jsp?recid=CN90101910.0&leixin=fmzl&title=检测光束准直的剪切干涉仪&ipc=G01M11/00</sourceUrl>" +
		"<encode>gb2312</encode><applyNum>90101910.0</applyNum><applyDate>1990.04.03</applyDate>" +
		"<name>检测光束准直的剪切干涉仪</name>" +
		"<publicNum>CN1055600</publicNum>" +
		"<publicDate>1991.10.23</publicDate>" +
		"<mainCategoryNum>G01M11/00</mainCategoryNum>" +
		"<categoryNum>G01M11/00;G02B27/30</categoryNum>" +
		"<awardDate></awardDate><priority></priority>" +
		"<applyPerson>中国科学院上海光学精密机械研究所</applyPerson>" +
		"<address>201800上海市加定县加定镇清河路390号</address>" +
		"<designer>徐德衍</designer>" +
		"<internationApply></internationApply>" +
		"<internationPublish></internationPublish><enterDate></enterDate>" +
		"<brokerOrg>中国科学院上海专利事务所</brokerOrg><broker>李兰英; 张泽纯</broker><abstracts>本发明属于光学仪器系统领域的一种检测光束准直的剪切干涉仪。用于光学与激光工程，光学与激光实验中光束准直的检测。由入射光阑屏、带有楔角的可以旋转３６０°的剪切干涉平板和图形观测屏三部分组成。这三部分既" +
		"可以设计成一体式的，也可以是分离式的。本发明的干涉仪具有检测灵敏度高，可检测较大光束口径，并能够直接给出定量的结果等优点。</abstracts></root>";		

ReadXMLFromString rxfs=new ReadXMLFromString();
rxfs.setSource(t);
try {
	rxfs.setSource(rxfs.getSource().replaceAll("&", "#amp"));
	System.out.println(rxfs.readerValueByNode("/root/sourceUrl").replaceAll("#amp", "&"));
	
} catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
	
	}
}
